package com.keshihao.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keshihao.commonutils.R;
import com.keshihao.eduservice.entity.EduTeacher;
import com.keshihao.eduservice.entity.vo.TeacherQuery;
import com.keshihao.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author keshihao
 * @since 2020-05-04
 */
@Api(description = "讲师管理")
@CrossOrigin
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    // 1.查询讲师表所有数据
    // rest风格
    @ApiOperation("讲师列表")
    @GetMapping("/findAll")
    public R findAll() {
        // 调用service的方法实现查询所有的方法
        List<EduTeacher> eduTeacherList = eduTeacherService.list(null);
        return R.ok().data("eduTeacherList",eduTeacherList);
    }

    // 2.逻辑删除讲师的方法
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("/{id}")
    public R removeTeacher(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if (flag) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    // 3.分页查询讲师的方法
    @GetMapping("pageTeacher/{current}/{limit}")
    @ApiOperation("讲师列表分页查询")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit){

        // 创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        // 调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        eduTeacherService.page(pageTeacher, null);

        List<EduTeacher> records = pageTeacher.getRecords();
        long total = pageTeacher.getTotal();

        Map map = new HashMap();
        map.put("total",total);
        map.put("records",records);

        return R.ok().data(map);
    }

    // 4.多条件组合查询带分页
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    @ApiOperation("多条件组合查询带分页")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){

        // 创建page对象
        Page<EduTeacher> eduTeacherPage = new Page<>(current,limit);

        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper();

        String name = teacherQuery.getName();

        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }

        if (!StringUtils.isEmpty(teacherQuery.getLevel())){
            queryWrapper.eq("level",teacherQuery.getLevel());
        }

        if (!StringUtils.isEmpty(teacherQuery.getBegin())){
            queryWrapper.ge("gmt_create",teacherQuery.getBegin());
        }
        if (!StringUtils.isEmpty(teacherQuery.getEnd())){
            queryWrapper.like("gmt_create",teacherQuery.getEnd());
        }

        eduTeacherService.page(eduTeacherPage,queryWrapper);

        List<EduTeacher> records = eduTeacherPage.getRecords();
        long total = eduTeacherPage.getTotal();

        Map map = new HashMap();
        map.put("total",total);
        map.put("records",records);
        return R.ok().data(map);
    }

    // 添加讲师接口
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }else{
            return R.error();
        }
    }

    // 根据讲师ID进行查询
    @GetMapping("getTeacher/{id}")
    @ApiOperation("根据讲师ID进行查询")
    public R getTeacher(@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    // 讲师修改功能
    @PostMapping("updateTeacher")
    @ApiOperation("讲师修改功能")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }


}

